//
//  FZPhotoView.swift
//  FZImageBrowser
//
//  Created by 李雷川 on 16/5/28.
//  Copyright © 2016年 李雷川. All rights reserved.
//

import UIKit

public class FZBrowseZoomScrollView: UIScrollView {
    public let imageView = UIImageView()
    public var tapClouse:(()->Void)?

    var limiteMAXHeight :Bool = false;//是否限制d
    
    override public func awakeFromNib() {
        buildUI()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        buildUI()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
       
    }
    
    func buildUI(){
        let tapGesture = UITapGestureRecognizer(target: self, action:#selector(tap(_:)))
        let doubleTapGesture = UITapGestureRecognizer(target: self, action:#selector(doubleTap(_:)))
        doubleTapGesture.numberOfTapsRequired = 2
   
        
        imageView.clipsToBounds = true
        imageView.contentMode = .scaleAspectFit
        imageView.isUserInteractionEnabled = true
        self.isScrollEnabled=false
        self.addGestureRecognizer(tapGesture)
        self.addGestureRecognizer(doubleTapGesture)
        tapGesture.require(toFail: doubleTapGesture)
        addSubview(imageView)
        self.maximumZoomScale = 3.0
        self.minimumZoomScale = 1.0
        
        self.delegate = self
    }
    
    @objc func tap(_ getsture:UITapGestureRecognizer){
        guard let doClouse = tapClouse else{
            return
        }
        doClouse()
    }
    //放大
    @objc func doubleTap(_ getsture:UITapGestureRecognizer){
        if(self.zoomScale > self.minimumZoomScale){
            self.setZoomScale(self.minimumZoomScale, animated: true)
        }
        else{
            let  width = frame.width/self.maximumZoomScale
            let  height = frame.height/self.maximumZoomScale
            let point = getsture.location(in: imageView)
            self.zoom(to: CGRect(x: point.x - width/2, y: point.y - height/2, width: width, height: height), animated: true)
        }
        
    }
    

  
    public  func setImage(_ image:UIImage){
        imageView.image = image
        imageView.frame = imageRectInScrollView(image)
    }
    
    func resetImageViewFrame(_ image:UIImage){
        imageView.image = image;
        imageView.frame = imageRectInScrollView(image)
        
    }
    
    func imageRectInScrollView(_ image:UIImage)->CGRect{
        let screenWidth = frame.width
        var screenHeight = frame.height
        if self.limiteMAXHeight == true{
            screenHeight = frame.height - 128;
        }
        
        let widthRatio = screenWidth/image.size.width
        let heightRatio = screenHeight/image.size.height
        
        let scale = min(widthRatio, heightRatio)
        let width  = image.size.width * scale
        let height = image.size.height * scale
        var rect:CGRect = CGRect.zero;
        
        if self.limiteMAXHeight {
            rect = CGRect(x: (screenWidth-width)/2,y: (frame.height-height)/2,width: width,height: height)
        }else{
            rect = CGRect(x: (screenWidth-width)/2,y: (screenHeight-height)/2,width: width,height: height)
        }
        
        return rect
    }
    
}

extension FZBrowseZoomScrollView:UIScrollViewDelegate{
    public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    
    public func scrollViewDidZoom(_ scrollView: UIScrollView) {
        var rect = imageView.frame
        rect.origin.x = 0
        rect.origin.y = 0
        if(rect.size.width < frame.width){
            let x = (frame.width - rect.size.width)/2
            rect.origin.x = CGFloat(floorf(Float(x)))
        }
        if(rect.size.height < frame.height){
            let y = (frame.height - rect.size.height)/2
            rect.origin.y = CGFloat(floorf(Float(y)))
        }
        imageView.frame = rect
        
    }
}
